{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bca3f789-8723-447e-b4b4-fd2098cee35d",
   "metadata": {},
   "source": [
    "# 任务\n",
    "对已经进行过缺省值填充和异常值替换的数据集进行特征选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d4c0da61-db1a-4b91-b3c8-89b7a6adec4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e8257729-892f-483e-8abb-f60f8b9b9e49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Selected features: Index(['CryoSleep', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa',\n",
      "       'VRDeck', 'HomePlanet_Earth', 'HomePlanet_Europa',\n",
      "       'Destination_55 Cancri e', 'Deck'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_selection import SelectKBest, f_classif\n",
    "\n",
    "train_data = pd.read_csv('../../RefreshedData/refreshed_train.csv')\n",
    "test_data = pd.read_csv('../../RefreshedData/refreshed_test.csv')\n",
    "\n",
    "X = train_data.iloc[:, :-1]\n",
    "y = train_data.iloc[:, -1]\n",
    "\n",
    "selector = SelectKBest(f_classif, k=10)  # 选前10个最相关特征\n",
    "X_new = selector.fit_transform(X, y)\n",
    "# 查看哪些特征被选中了\n",
    "mask = selector.get_support() \n",
    "selected_features = X.columns[mask]\n",
    "train_selected_data = list(mask)\n",
    "test_selected_data = list(mask)\n",
    "train_selected_data.append(True)\n",
    "selected_train = train_data.iloc[:, train_selected_data]\n",
    "selected_test = test_data.iloc[:, test_selected_data]\n",
    "\n",
    "# 将特征选择后的文件输出\n",
    "selected_train.to_csv('../../FeatureSelectedData/selected_train.csv', index=False)\n",
    "selected_test.to_csv('../../FeatureSelectedData/selected_test.csv', index=False)\n",
    "\n",
    "print(\"Selected features:\", selected_features)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76ada0b1-a56d-480d-94e4-ea148e58fdcb",
   "metadata": {},
   "source": [
    "### 运行代码，选择好的数据集将保存到 `FeatureSelectedData` 目录下"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
